using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using NaughtyAttributes;

public class SelectableObject : MonoBehaviour
{
    public bool ShowEffectGizmos;
    //outline
    public Color outlineColor;
    //health bar
    public Vector3 hudLocalPos = new Vector3(0, 2, 0);
    //projector
    public Vector3 projectorLocalPos = new Vector3(0, 0, 0);
    public float projectorRadius = 0.5f;
    public Color projectorColor = Color.green;

    public bool ShowMeshGizmos;
    public GameObject mainBody;
    public bool useCombine;

    private MeshBoundingBox boundingBox;

    private BoxCollider boxCollider;

    private void Awake()
    {
        GenerateMeshBoundingBox();
        GenerateBoxCollider();
    }

    public Rect GetRectInScreen()
    {
        return boundingBox.GetScreenRect();

        //return Utils.GetScreenRect(Utils.GetBoundsVertex(boxCollider.bounds));
    }

    [Button("GenerateMeshBoundingBox")]
    private void GenerateMeshBoundingBox()
    {
        Mesh mesh = GetBoundingMesh();
        boundingBox = new MeshBoundingBox(mainBody.transform, mesh.bounds.center, mesh.bounds.size / 2);
    }

    [Button("GenerateBoxCollider")]
    private void GenerateBoxCollider()
    {
        Mesh mesh = GetBoundingMesh();
        boxCollider = gameObject.AddAndGetComponent<BoxCollider>();
        boxCollider.isTrigger = true;
        boxCollider.center = mesh.bounds.center - mainBody.transform.position;
        boxCollider.size = mesh.bounds.size;
    }

    private Mesh GetBoundingMesh() 
    {
        Mesh mesh;
        if (useCombine)
        {
            mesh = Utils.GetWorldCombineMesh(gameObject);
        }
        else
        {
            mesh = Utils.GetWorldMesh(mainBody);
        }
        return mesh;
    }

    private void OnDrawGizmos()
    {
        if (ShowMeshGizmos) 
        {
            if (boundingBox != null) 
            {
                boundingBox.DrawBoundingBox();
            }
        }

        if (ShowEffectGizmos) 
        {
            Gizmos.DrawSphere(transform.position + hudLocalPos, 0.1f);
            Gizmos.DrawSphere(transform.position + projectorLocalPos, 0.1f);
        }
    }
}
